package com.tjmc.tiomsresource.service.impl;

import com.google.common.collect.Lists;
import com.tjmc.tiomscore.dao.*;
import com.tjmc.tiomscore.model.*;
import com.tjmc.tiomsresource.parser.CMTplParser;
import com.tjmc.tiomsresource.service.CMTplService;
import com.tjmc.tiomsresource.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-20 15:41
 */
@Service("cmTplService")
public class CMTplServiceImpl extends CMSuperServiceImpl<TranPtnHwTpl> implements CMTplService {

    @Autowired
    private CMTplParser cmTplParser;
    @Autowired
    private TranPtnHwTplMapper tranPtnHwTplMapper;
    @Autowired
    private TiTplMapper tiTplMapper;
    @Autowired
    private TranPtnTplMapper tranPtnTplMapper;
    @Autowired
    private TiNelMapper tiNelMapper;
    @Autowired
    private TiPortMapper tiPortMapper;

    @Override
    public CMTplParser getParser() {
        return cmTplParser;
    }

    @Override
    public TranPtnHwTplMapper getMapper() {
        return tranPtnHwTplMapper;
    }

    @Override
    public boolean saveTranPtnHwDataToDB(List<File> xmlFiles) {
        return this.saveTranPtnHwDataToDB(xmlFiles, "TPL");
    }


    @Override
    public void buildTiTplToDB() {
        tiTplMapper.truncateTable();
        tiTplMapper.insertDataFromTranPtnTpl();
    }

    @Override
    public void buildTiTplToDB2() {
        tiTplMapper.truncateTable();
        List<TranPtnTpl> tranPtnTplList = tranPtnTplMapper.selectByExample(null);
        TiNelExample tiNelExample;
        TiPortExample tiPortExample;
        List<TiTpl> tiTplList = new ArrayList<>();
        for(TranPtnTpl tranPtnTpl : tranPtnTplList) {
            TiTpl tiTpl = new TiTpl();
            tiNelExample = new TiNelExample();
            tiNelExample.createCriteria().andRmuidEqualTo(tranPtnTpl.getAendnermuid());
            List<TiNel> tiNels = tiNelMapper.selectByExample(tiNelExample);
            if(null == tiNels || tiNels.size() != 1) {
                continue;
            }
            tiTpl.setAnelId(tiNels.get(0).getId());
            tiNelExample = new TiNelExample();
            tiNelExample.createCriteria().andRmuidEqualTo(tranPtnTpl.getZendnermuid());
            tiNels = tiNelMapper.selectByExample(tiNelExample);
            if(null == tiNels || tiNels.size() != 1) {
                continue;
            }
            tiTpl.setZnelId(tiNels.get(0).getId());
            tiPortExample = new TiPortExample();
            tiPortExample.createCriteria().andRmuidEqualTo(tranPtnTpl.getAendportrmuid());
            List<TiPort> tiPorts = tiPortMapper.selectByExample(tiPortExample);
            if(null == tiPorts || tiPorts.size() != 1) {
                continue;
            }
            tiTpl.setAportId(tiPorts.get(0).getId());
            tiPortExample = new TiPortExample();
            tiPortExample.createCriteria().andRmuidEqualTo(tranPtnTpl.getZendportrmuid());
            tiPorts = tiPortMapper.selectByExample(tiPortExample);
            if(null == tiPorts || tiPorts.size() != 1) {
                continue;
            }
            tiTpl.setZportId(tiPorts.get(0).getId());
            tiTpl.setRmuid(tranPtnTpl.getRmuid());
            tiTpl.setNativename(tranPtnTpl.getNativename());
            tiTpl.setDirection(tranPtnTpl.getDirection());
            tiTpl.setRate(tranPtnTpl.getRate());
            tiTpl.setTranPtnTplId(tranPtnTpl.getId());
            tiTplList.add(tiTpl);
        }
        if(tiTplList.size() != 0) {
            List<List<TiTpl>> lists = Lists.partition(tiTplList, Constants.BATCH_SPLIT_SIZE);
            for(List<TiTpl> tiTpls : lists) {
                tiTplMapper.insertBatch(tiTpls);
            }
        }
    }
}
